package com.dhcc.core.framework.security.client.cas;

import com.dhcc.core.config.properties.SsoProperties;

import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.WebContext;

public class CasConfiguration extends org.pac4j.cas.config.CasConfiguration {

    @Override
    public String computeFinalLoginUrl(final WebContext context) {
        init();
        String url = this.getLoginUrl();
        final boolean relativeUrl = url != null
            && !url.startsWith(HttpConstants.SCHEME_HTTP) && !url.startsWith(HttpConstants.SCHEME_HTTPS);
        if (context != null && relativeUrl) {
            return SsoProperties.me().getDynamicServerUrl()+(url.startsWith("/") ? url : "/" + url);
        }
        return url;
    }

    @Override
    public String computeFinalPrefixUrl(final WebContext context) {
        init();
        String url = this.getPrefixUrl();
        final boolean relativeUrl = url != null
            && !url.startsWith(HttpConstants.SCHEME_HTTP) && !url.startsWith(HttpConstants.SCHEME_HTTPS);
        if (context != null && relativeUrl) {
            return SsoProperties.me().getServerUrl()+(url.startsWith("/") ? url : "/" + url);
        }
        return url;
    }
    
    @Override
    public String computeFinalRestUrl(final WebContext context) {
        init();
        String url = this.getRestUrl();
        final boolean relativeUrl = url != null
            && !url.startsWith(HttpConstants.SCHEME_HTTP) && !url.startsWith(HttpConstants.SCHEME_HTTPS);
        if (context != null && relativeUrl) {
            return SsoProperties.me().getServerUrl()+(url.startsWith("/") ? url : "/" + url);
        }
        return url;
    }
}